/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sack.picasawebalbums;

import com.google.gdata.data.Content;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.photos.AlbumFeed;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.openide.util.Exceptions;
import com.google.gdata.data.photos.PhotoEntry;
import java.io.File;

/**
 *
 * @author admin
 */
class Photo {
    private Feeds feed;
    private PhotoEntry photoentry;
    private String description;
    private File imageFile;
    private AlbumFeed albumfeed;
    private String albumname;
    static String path = java.lang.System.getProperty("user.home")+"/PicasaAlbums";
    private String photosize = "576";
    String photopath;
    
    Photo(String description,Feeds feed, PhotoEntry photoentry,AlbumFeed albumfeed) {
        this.description =description;
        this.feed = feed;
        this.photoentry = photoentry;
        this.albumfeed = albumfeed;
        this.albumname = albumfeed.getName();
    }
    void setPhotoSize(String size){
        this.photosize = size;
    }
    Feeds getFeed(){
        return feed;
    }
    PhotoEntry getPhotoEntry(){
        return photoentry;
    }
    @Override
    public String toString(){
        return photoentry.getTitle().getPlainText();
    }
    File getPhotoImage(PicasaAlbumTopComponent aThis)throws java.net.SocketException {
        setPhotoSize(aThis.photosize);
        Content c = photoentry.getContent();
        if(c instanceof MediaContent){
            try {
                MediaContent mc = (MediaContent) c;
                URL imageUrl = new URL(mc.getUri());
                imageFile = getImage(imageUrl);
            } catch (MalformedURLException ex) {
                Exceptions.printStackTrace(ex);
            }
       }
       return imageFile;
    }
    private File getImage(URL imageURL)throws java.net.SocketException {
        File photopathFile = new File(getPhotopath()) ;
        if(!photopathFile.exists()){
            InputStream inputStream = null;
            try {
            inputStream = imageURL.openStream();
            if (inputStream != null) {
                try {
                    imageFile = photopathFile;
                    FileOutputStream fos = new FileOutputStream(imageFile);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) > 0) {
                        fos.write(buffer, 0, bytesRead);
                    }
                    fos.close();
              }catch (Exception ex) {
                    if (imageFile != null) {
                        if (imageFile.exists()) 
                            imageFile.delete();
                        imageFile = null;
                    }
                }
            }
            } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            try {
                inputStream.close();
            } 
            
            catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
    }
            return imageFile;
        }
        else{
            return photopathFile;
        }
    }
    private String getPhotopath() {
        photopath = path+"/"+albumname+"/"+photosize+photoentry.getTitle().getPlainText();
        return photopath;
    }
}